//
//  CLSettingCell.m
//  togetherLBS
//
//  Created by louis on 15/9/12.
//  Copyright (c) 2015年 celi. All rights reserved.
//

#import "CLSettingCell.h"
#import "CLSettingItem.h"
#import "CLSettingArrowItem.h"
#import "CLSettingSwitchItem.h"
#import "CLSettingIconItem.h"
#import "CLSettingButtonItem.h"
#import "UIImage+Fit.h"

@interface CLSettingCell()
@property (nonatomic, strong) UISwitch *switchView;
@property (nonatomic, strong) UIColor *detialLabelDefaultColor;
@property (nonatomic, strong) UIFont *detialLabelDefaultFont;
@property (nonatomic, weak) UIImageView *iconView;
@property (nonatomic, weak) UIButton *deleteButton;
@end

@implementation CLSettingCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *cellID = @"setting";
    
    CLSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[CLSettingCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
        ;
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 初始化操作
        self.detialLabelDefaultColor = self.detailTextLabel.textColor;
        self.detialLabelDefaultFont = self.detailTextLabel.font;
        [self.contentView addSubview:self.iconView];
    }
    return self;
}

- (UISwitch *)switchView
{
    if (_switchView == nil) {
        _switchView = [[UISwitch alloc] init];
        [_switchView addTarget:self action:@selector(switchStateChange) forControlEvents:UIControlEventValueChanged];
    }
    return _switchView;
}

- (UIImageView *)iconView
{
    if (_iconView == nil) {
        UIImageView *imageView = [[UIImageView alloc] init];
        _iconView = imageView;
        _iconView.hidden = YES;
        
        _iconView.layer.cornerRadius = 8.0;
        _iconView.layer.masksToBounds = YES;
        _iconView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        _iconView.layer.borderWidth = 1.0;
    }
    return _iconView;
}

- (UIButton *)deleteButton
{
    if (_deleteButton == nil) {
        UIButton *delete = [UIButton buttonWithType:UIButtonTypeCustom];
        delete.hidden = YES;
        // 图片
        [delete setBackgroundImage:[UIImage resizeImage:@"RedButton"] forState:UIControlStateNormal];
        [delete setBackgroundImage:[UIImage resizeImage:@"RedButtonPressed"] forState:UIControlStateHighlighted];
        [delete addTarget:self action:@selector(deleteBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        _deleteButton = delete;
        [self.contentView addSubview:delete];
    }
    return _deleteButton;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat iconW = 60;
    CGFloat iconH = 60;
    CGFloat iconX = self.contentView.frame.size.width - iconW;
    CGFloat iconY = (self.contentView.frame.size.height - iconH) * 0.5;
    self.iconView.frame = CGRectMake(iconX, iconY, iconW, iconH);
    
    self.deleteButton.frame = self.contentView.bounds;
}

- (void)setItem:(CLSettingItem *)item
{
    _item = item;
    
    // 1. 初始化数据
    [self setupData];
    
    // 2. 设置右边的数据
    [self setupRightData];
}

- (void)setupRightData
{
    if ([self.item isKindOfClass:[CLSettingSwitchItem class]]) {
        self.accessoryView = self.switchView;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        // 设置开关的状态
        self.switchView.on = [(CLSettingSwitchItem *)(self.item) isOn];
        
    } else if([self.item isKindOfClass:[CLSettingArrowItem class]]) {
        self.accessoryView = nil;
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
    } else if([self.item isKindOfClass:[CLSettingIconItem class]]){
        
        self.accessoryView = nil;
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
    } else {
        self.accessoryView = nil;
        self.accessoryType = UITableViewCellAccessoryNone;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
}

- (void)setupData
{
    self.textLabel.text = self.item.title;
    self.detailTextLabel.text = self.item.subtitle;
    if (self.item.icon) {
        self.imageView.image = [UIImage imageNamed:self.item.icon];
    }
    
    if (self.item.subTitleFont) {
        self.detailTextLabel.font = self.item.subTitleFont;
    } else {
        self.detailTextLabel.font = self.detialLabelDefaultFont;
    }
    
    if ([self.item isKindOfClass:[CLSettingIconItem class]]) {
        self.iconView.hidden = NO;
        self.deleteButton.hidden = YES;
        self.iconView.image = [(CLSettingIconItem *)self.item image];
    } else if ([self.item isKindOfClass:[CLSettingButtonItem class]]){
        self.iconView.hidden = YES;
        self.deleteButton.hidden = NO;
        // 标题
        CLSettingButtonItem *item = (CLSettingButtonItem *)self.item;
        NSString *title = item.buttonTitle.length > 0 ? item.buttonTitle : NSLocalizedString(@"Delete", nil);
        [self.deleteButton setTitle:title forState:UIControlStateNormal];
    } else {
        self.iconView.hidden = YES;
        self.deleteButton.hidden = YES;
    }
    
    if ([self.item.subtitle isEqualToString:NSLocalizedString(@"NotSet", nil)]) {
        self.detailTextLabel.textColor = [UIColor redColor];
    } else if (self.item.subTitleColor){
        self.detailTextLabel.textColor = self.item.subTitleColor;
    } else {
        self.detailTextLabel.textColor = self.detialLabelDefaultColor;
    }
    
}

- (void)switchStateChange
{
    //如果cell背景R G B的色值为DYColor(235, 235, 242)点击开关不起效
    UIColor *byColor = self.backgroundColor;
    bool isColor = CGColorEqualToColor( byColor.CGColor, DYColor(235, 235, 242).CGColor);
    if (isColor) {
        return;
    }
    
    if ([self.delegate respondsToSelector:@selector(settingCell:didChangeSwitchState:)]) {
        [self.delegate settingCell:self didChangeSwitchState:self.switchView.on];
    }
}

- (void)deleteBtnClicked
{
    if ([self.delegate respondsToSelector:@selector(settingCellDidClickButton:)]) {
        [self.delegate settingCellDidClickButton:self];
    }
}
@end
